<template>
  <div class="doajishi" v-if="tiao">{{s}}</div>
  <div class="doajishitiao" v-else @click="tiaofun">跳过</div>
  <div class="qidongye">
    <img :src="img[refval]">
  </div>
</template>

<script >
import { onUnmounted, reactive, toRefs, watch, ref } from "vue";
import { Post } from "@/api/axios";
import $router from "../router/index";
import FingerprintJS from "@fingerprintjs/fingerprintjs";
export default {
  name: " ",
  setup() {
    const data = reactive({
      s: 5,
      url: "",
      bool: false,
      tiao: true,
    });

    if (!window.isKey.includes(window.location.host.split(".")[0])) {
      data.tiao = false;
    }

    const tiaofun = () => {
      if (window.isKey.includes(window.location.host.split(".")[0])) {
        if (data.url) {
          window.location.href = data.url;
          // $router.push({
          //   path: "/Iframe",
          //   query: { url: data.url, adv: true },
          // });
        } else {
          $router.replace("/home");
        }
      } else {
        $router.replace("/home");
      }
    };

    let time;
    const init = () => {
      if (time) {
        clearInterval(time);
      }
      time = setInterval(() => {
        if (data.s > 0) {
          data.s--;
        }
        if (data.s === 0) {
          if (window.isKey.includes(window.location.host.split(".")[0])) {
            if (data.url) {
              window.location.href = data.url;
              // $router.push({
              //   path: "/Iframe",
              //   query: { url: data.url, adv: true },
              // });
            } else {
              if (data.bool) {
                $router.replace("/home");
              }
            }
          } else {
            $router.replace("/home");
          }
          clearInterval(time);
        }
      }, 1000);
    };
    init();

    watch(
      () => data.bool,
      (count) => {
        if (count) {
          data.tiao = false;
          if (data.s === 0) {
            if (window.isKey) {
              if (data.url) {
                window.location.href = data.url;
                // $router.push({
                //   path: "/Iframe",
                //   query: { url: data.url, adv: true },
                // });
              } else {
                $router.replace("/home");
              }
            } else {
              $router.replace("/home");
            }
          }
        }
      }
    );

    const random = reactive({
      img: [
        require("../assets/qidongye/01.png"),
        require("../assets/qidongye/02.png"),
        require("../assets/qidongye/03.png"),
        require("../assets/qidongye/04.png"),
        require("../assets/qidongye/05.png"),
      ],
    });
    const refval = ref(0);
    const randomimg = () => {
      refval.value = Math.floor(Math.random() * random.img.length);
    };
    randomimg();

    onUnmounted(() => {
      clearInterval(time);
    });

    const Fingerprint = async () => {
      const fp = await FingerprintJS.load();
      const result = await fp.get();
      localStorage.setItem("visitorId", result.visitorId);
      return result.visitorId;
    };

    window.mui.init();
    window.mui.plusReady(() => {
      var Intent = window.plus.android.getAttribute("unScreens", "Prohibit");
      console.log(Intent);
      //获取检测权限的状态
      localStorage.setItem("phoneType", window.plus.os.name);
      if ("Android" == window.plus.os.name) {
        window.plus.device.getInfo({
          success: (e) => {
            /* 获取安卓id */
            let Settings = window.plus.android.importClass(
              "android.provider.Settings"
            );
            let main = window.plus.android.runtimeMainActivity();
            let ANDROIDID = Settings.Secure.getString(
              main.getContentResolver(),
              Settings.Secure.ANDROID_ID
            );
            e.ANDROIDID = ANDROIDID;

            /* 获取安卓OAID */
            window.plus.device.getOAID({
              success: function (oaid) {
                e.oaid = oaid.oaid;
                /* 获取安卓imei */
                localStorage.setItem("imei", JSON.stringify(e));
                Post({
                  url: "index/clickJihuo",
                  data: {
                    platform: "android",
                    imei: JSON.stringify(e),
                    visitorId: localStorage.getItem("visitorId"),
                  },
                }).then((result) => {
                  if (window.isKey.length > 0) {
                    data.bool = true;
                  }
                  if (result) {
                    if (result.data) {
                      data.url = result.data;
                    }
                  }
                });
              },
              fail: function () {
                /* 获取安卓imei */
                localStorage.setItem("imei", JSON.stringify(e));
                Post({
                  url: "index/clickJihuo",
                  data: {
                    platform: "android",
                    imei: JSON.stringify(e),
                    visitorId: localStorage.getItem("visitorId"),
                  },
                }).then((result) => {
                  if (window.isKey.length > 0) {
                    data.bool = true;
                  }
                  if (result) {
                    if (result.data) {
                      data.url = result.data;
                    }
                  }
                });
              },
            });
          },
        });
      }
      if ("iOS" == window.plus.os.name) {
        window.plus.ios.importClass("NSUUID");
        var UIDevice = window.plus.ios.importClass("UIDevice");
        var currentDevice = UIDevice.currentDevice();
        currentDevice.identifierForVendor().UUIDString();
        var ASIdentifierManager = window.plus.ios.importClass(
          "ASIdentifierManager"
        );
        var sharedManager = ASIdentifierManager.sharedManager();
        var advertisingIdentifier = sharedManager.advertisingIdentifier();
        var idfa = window.plus.ios.invoke(advertisingIdentifier, "UUIDString");
        localStorage.setItem("idfa", idfa);
        Post({
          url: "index/clickJihuo",
          data: {
            platform: "ios",
            imei: JSON.stringify({ idfa }),
            visitorId: localStorage.getItem("visitorId"),
          },
        }).then((result) => {
          if (window.isKey.length > 0) {
            data.bool = true;
          }
          if (result) {
            if (result.data) {
              data.url = result.data;
            }
          }
        });
      }
    });

    if (!window.plus) {
      if (
        window.isWeb.includes(window.location.host.split(".")[0]) ||
        window.isKey.includes(window.location.host.split(".")[0])
      ) {
        (async () => {
          const imei = { imei: await Fingerprint(), type: "web" };
          localStorage.setItem("phoneType", "web");
          localStorage.setItem("imei", JSON.stringify(imei));
          Post({
            url: "index/clickJihuo",
            data: {
              platform: "web",
              imei: JSON.stringify(imei),
              visitorId: await Fingerprint(),
            },
          }).then((result) => {
            if (window.isKey.length > 0) {
              data.bool = true;
            }
            if (result) {
              if (result.data) {
                data.url = result.data;
              }
            }
          });
        })();
      }
    }

    return {
      ...toRefs(data),
      ...toRefs(random),
      refval,
      tiaofun,
      randomimg,
    };
  },
};
</script>
<style lang="scss" scoped>
.doajishi,
.doajishitiao {
  position: fixed;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  top: 20px;
  right: 20px;
  background: #666;
  color: white;
  font-size: 16px;
  text-align: center;
  line-height: 33px;
}
.doajishitiao {
  font-size: 11px;
}
.qidongye {
  img {
    width: 100%;
  }
}
</style>